package lumis.content.table;

import java.util.ArrayList;
import java.util.Collection;

import lumis.content.ContentManagerFactory;
import lumis.content.core.Content;
import lumis.content.core.ContentAssociation;
import lumis.content.core.ContentVersion;
import lumis.content.source.field.MetaDataSourceField;
import lumis.doui.processaction.ProcessActionHandler;
import lumis.doui.source.SourceContext;
import lumis.doui.source.SourceFactory;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.util.XmlUtil;

/**
 * Process action handler for content associations.
 * <p>
 * This process action handler assumes that associations were specified using a 
 * dynamic list control.
 * </p>
 * <p>
 * This process action handler may be used generically for associations
 * of different types.
 * </p>
 *
 * @version $Revision: 9601 $ $Date: 2008-07-18 15:38:17 -0300 (Fri, 18 Jul 2008) $
 * @since 4.2.0
 */
public class ContentAssociationProcessActionHandler extends ProcessActionHandler<ContentTableSource>
{
	protected String associationType = ContentAssociation.TYPE_STANDARD;
	protected ContentVersion contentVersion = null;
	protected ContentTableSource contentTableSource = null;
	
	public void processAction() throws PortalException
	{
		associationType = XmlUtil.readNodeString("associationType", processActionNode, associationType);
		String contentVersionId = this.douiContext.getRequest().getAttribute(ContentMetaDataController.CONTENT_VERSION_ID_ATTRIBUTE).toString();
		contentVersion = ManagerFactory.getEntityManager().find(ContentVersion.class, contentVersionId);
		Content curContent = contentVersion.getContentLocale().getContent();
		String sourceId = curContent.getSourceId();
		String serviceInstanceId = curContent.getServiceInstanceId();
		SourceContext sourceContext = new SourceContext(SessionConfig.getCurrentSessionConfig(), ManagerFactory.getServiceInstanceManager().get(serviceInstanceId, transaction), this.transaction);
		contentTableSource = (ContentTableSource)SourceFactory.createSource(sourceId, sourceContext);
		
		String dynamicListControlId = XmlUtil.readNodeString("dynamicListControlId", processActionNode);
		if(dynamicListControlId == null)
			throw new UnexpectedException("dynamicListControlId not specified.");
		
		// if versioning is supported, do not delete anything
		if (contentTableSource.getField(MetaDataSourceField.Type.VersionActive) == null)
		{
			// versioning not supported, delete previous associations with the same type.
			// (create another collection to prevent ConcurrentModificationException)
			Collection<ContentAssociation> contentAssociations = new ArrayList<ContentAssociation>(contentVersion.getContentAssociations());
			for(ContentAssociation contentAssociation: contentAssociations)
			{
				if (associationType.equals(contentAssociation.getType()))
					contentAssociation.delete();
			}
		}
		
		// now add new associations
		String[] associatedContentIds = (String[]) getParameter(dynamicListControlId+".items");
		if(associatedContentIds != null)
		{
			for(String associatedContentId : associatedContentIds)
			{
				Content associatedContent = ContentManagerFactory.getContentManager().getContent(associatedContentId);
				ContentAssociation contentAssociation = contentVersion.addContentAssociation();
				contentAssociation.setAssociatedContent(associatedContent);
				contentAssociation.setType(associationType);
			}
		}
	}
}
